/**
 * 
 */
package com.easidea.tim.dao.system.orgnization;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springside.core.dao.extend.HibernateEntityExtendDao;

import com.easidea.tim.entity.system.orgnization.Staff;
import com.easidea.tim.util.ParamsUtilities;

/**
 * @author Harrison
 * 
 */
public class StaffDAO extends HibernateEntityExtendDao<Staff> {
	private StaffTeamworkDao staffTeamworkDao;

	public Staff getStaffbyName(String name) {
		Criteria ca = createUnDeletedCriteria();
		ca.add(Restrictions.eq("username", name));
		ca.setMaxResults(1);
		return (Staff) ca.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public String getSelfCode(Long id) {
		String hql = "select selfCode from " + Staff.class.getName()
				+ " where id = " + id;
		List<String> obj = this.getHibernateTemplate().find(hql);
		if (obj != null && obj.size() > 0) {
			return obj.get(0);
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public List<Staff> getStaffListbyName(String name) {
		String hql = "select new com.easidea.tim.entity.system.orgnization.Staff(s.id,s.name,s.username)"
				+ " from "
				+ Staff.class.getName()
				+ " as s where s.deleted = 0 and s.name like '"
				+ ParamsUtilities.dealWithReservedSqlChar(name) + "%'";
		return this.getHibernateTemplate().find(hql);
		// Criteria ca = this.createUnDeletedCriteria();
		// ProjectionList proList = Projections.projectionList();//
		// proList.add(Projections.groupProperty("id"));
		// proList.add(Projections.groupProperty("name"));
		// proList.add(Projections.groupProperty("username"));
		// ca.setProjection(proList);
		// ca.add(Restrictions.ilike("name", name, MatchMode.START));
		// List<Object[]> objs = ca.list();
		// List<Staff> staffs = new ArrayList<Staff>();
		// for (Object[] obj : objs) {
		// Staff staff = new Staff((Long)obj[0],(String)obj[1],(String)obj[2]);
		// staffs.add(staff);
		// }
		// return staffs;
	}

	public List<Staff> getStaffsByDeptId(Long deptId) {
		Criteria ca = createUnDeletedCriteria();
		ca.add(Restrictions.eq("department.id", deptId));
		return ca.list();
	}
	
	/**
	 * 根据当前登录用户提取对应的协作人员ID
	 * 
	 * @param loginUserId
	 * @return
	 * @author Harrison
	 */
	public List<Long> getTeamworkStaffIdsByLoginUserId(Long loginUserId) {
		Staff staff = this.get(loginUserId);
		List<Long> staffIds = new ArrayList<Long>();
		String hql = "";
		List<Long> teamworkIds = this.staffTeamworkDao
				.getTeamworkIdByParentId(loginUserId);
		short recursionSearch = staff.getRecursionSearch() == null ? 0 : staff
				.getRecursionSearch();
		if (teamworkIds != null && teamworkIds.size() > 0) {

			if (recursionSearch == 0) {
				teamworkIds.add(loginUserId);
//				String ids = "";
//				for (Long id : teamworkIds) {
//					ids += id + ",";
//				}
//				ids = ids.substring(0, ids.length() - 1);
//				hql = " select id from " + Staff.class.getName()
//						+ " where id in (" + ids + ")";
//				staffIds = this.getHibernateTemplate().find(hql);
				staffIds = teamworkIds;
			} else {
				List<Long> totalList = new ArrayList<Long>();

				this.recursion(loginUserId, teamworkIds, totalList);
				totalList.add(loginUserId);
//				String ids = "";
//				for (Long id : totalList) {
//					ids += id + ",";
//				}
//				ids = ids.substring(0, ids.length() - 1);
//				hql = " select id from " + Staff.class.getName()
//						+ " where id in (" + ids + ")";
//				staffIds = this.getHibernateTemplate().find(hql);
				staffIds = totalList;
			}
		} else {
			staffIds.add(staff.getId());
		}
		return staffIds;
	}

	@SuppressWarnings("unchecked")
	public List<String> getTeamworkIdsByLoginUserId(Long loginUserId) {
		Staff staff = this.get(loginUserId);
		List<String> userNames = new ArrayList<String>();
		String hql = "";
		List<Long> teamworkIds = this.staffTeamworkDao
				.getTeamworkIdByParentId(loginUserId);
		short recursionSearch = staff.getRecursionSearch() == null ? 0 : staff
				.getRecursionSearch();
		if (teamworkIds != null && teamworkIds.size() > 0) {

			if (recursionSearch == 0) {
				teamworkIds.add(loginUserId);
				String ids = "";
				for (Long id : teamworkIds) {
					ids += id + ",";
				}
				ids = ids.substring(0, ids.length() - 1);
				hql = " select username from " + Staff.class.getName()
						+ " where id in (" + ids + ")";
				userNames = this.getHibernateTemplate().find(hql);
			} else {
				List<Long> totalList = new ArrayList<Long>();

				this.recursion(loginUserId, teamworkIds, totalList);
				totalList.add(loginUserId);
				String ids = "";
				for (Long id : totalList) {
					ids += id + ",";
				}
				ids = ids.substring(0, ids.length() - 1);
				hql = " select username from " + Staff.class.getName()
						+ " where id in (" + ids + ")";
				userNames = this.getHibernateTemplate().find(hql);
			}
		} else {
			userNames.add(staff.getUsername());
		}
		return userNames;
	}

	private void recursion(Long loginUserId, List<Long> teamworkIds,
			List<Long> totalList) {

		for (Long teamworkId : teamworkIds) {
			if (teamworkId.intValue() == loginUserId.intValue()) {
				continue;
			}
			if (totalList.contains(teamworkId)) {
				continue;
			}
			if (!totalList.contains(teamworkId))
				totalList.add(teamworkId);
			List<Long> newTeamworkIds = this.staffTeamworkDao
					.getTeamworkIdByParentId(teamworkId);
			if (newTeamworkIds != null && newTeamworkIds.size() > 0) {
				this.recursion(loginUserId, newTeamworkIds, totalList);
			}

		}
	}

	public void setStaffTeamworkDao(StaffTeamworkDao staffTeamworkDao) {
		this.staffTeamworkDao = staffTeamworkDao;
	}

}
